package its.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Properties;

public class MultipleConfigs {

	private static final String TEMPLATE_DIR = "_<TEMPLATE_DIR>_";
	private static final String TEMPLATE_FILE = "_<TEMPLATE_FILE>_";

	private static final class MatchFilenameFilter implements FilenameFilter {
		private String confFileName;

		private MatchFilenameFilter(String confFileName) {
			this.confFileName = confFileName;
		}

		@Override
		public boolean accept(File dir, String name) {
			return name.contains(confFileName);
		}
	}

	private static final class ConfFilenameFilter implements FilenameFilter {
		@Override
		public boolean accept(File dir, String name) {
			return name.endsWith(".conf");
		}
	}

	public static void main(String[] args) throws IOException {
		File d100 = new File("resources/100");
		File d200 = new File("resources/200");
		File d400 = new File("resources/400");
		File dResources = new File("resources");
		File dTemplates = new File("resources/templates");

		for (File dir : new File[] { d100, d200, d400 }) {
			String saturation = dir.getName().substring(
					dir.getName().indexOf(File.separatorChar) + 1);
			for (File fd : dTemplates.listFiles(new ConfFilenameFilter())) {
				System.out.println(fd.getName());
				Properties p = new Properties();
				FileInputStream fis = null;
				try {
					fis = new FileInputStream(fd);
					p.load(fis);
				} finally {
					if (fis != null) {
						fis.close();
					}
				}
				String confFilenameWithExt = fd.getName();
				String confFileName = confFilenameWithExt.substring(0,
						confFilenameWithExt.indexOf('.'));

				int i = 1;
				for (File f : dir.listFiles(new MatchFilenameFilter(confFileName))) {

					String confFilenameReplaced = confFileName + "-" + saturation + "-"
							+ i + ".conf";
					System.out.print(f.getName() + " -> ");
					System.out.println(confFilenameReplaced);

					File file = new File(dResources, confFilenameReplaced);
					if (file.createNewFile()) {
						storeFile(saturation, p, f, file);
					}
					++i;
				}
			}

		}

	}

	private static void storeFile(
			String saturation,
			Properties templateProperties,
			File templateConfigFile,
			File fileToStore)
	throws 
		FileNotFoundException,
		IOException
	{
		Properties newP = new Properties(templateProperties);
		
		String generatorPath = newP.getProperty("generator");
		generatorPath = generatorPath.replaceAll(TEMPLATE_DIR, saturation);
		generatorPath = generatorPath.replaceAll(TEMPLATE_FILE, templateConfigFile
				.getName());
		
		newP.setProperty("model", templateProperties.getProperty("model"));
		newP.setProperty("generator", generatorPath);
		newP.setProperty("shapes", templateProperties.getProperty("shapes"));

		FileOutputStream fos = null;
		try
		{
			fos = new FileOutputStream(fileToStore);
			newP.store(fos, "");
		}
		finally
		{
			if (fos != null)
			{
				fos.close();
			}
		}
		System.out.println("Successfully stored: " + fileToStore.getName());
	}
}
